package com.softtown.modules.dapai.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.enums.IsDeleteEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.dapai.dao.CompLeaseManageDao;
import com.softtown.modules.dapai.dao.CompLeaseOrderDao;
import com.softtown.modules.dapai.dto.CompLeaseManagePageDTO;
import com.softtown.modules.dapai.dto.CompLeaseOrderPageDTO;
import com.softtown.modules.dapai.dto.LeaseManageAddOrUpdateDTO;
import com.softtown.modules.dapai.dto.LeaseOrderAddOrUpdateDTO;
import com.softtown.modules.dapai.entity.CompLeaseManageDetailEntity;
import com.softtown.modules.dapai.entity.CompLeaseManageEntity;
import com.softtown.modules.dapai.entity.CompLeaseOrderDetailEntity;
import com.softtown.modules.dapai.entity.CompLeaseOrderEntity;
import com.softtown.modules.dapai.service.CompLeaseManageDetailService;
import com.softtown.modules.dapai.service.CompLeaseManageService;
import com.softtown.modules.dapai.service.CompLeaseOrderDetailService;
import com.softtown.modules.dapai.service.CompLeaseOrderService;
import com.softtown.modules.dapai.vo.CompLeaseManageVO;
import com.softtown.modules.dapai.vo.CompLeaseOrderVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CompLeaseMangeServiceImpl extends
        BaseServiceImpl<CompLeaseManageDao, CompLeaseManageEntity> implements CompLeaseManageService {


    @Resource
    private CompLeaseManageDetailService compLeaseManageDetailService;

    @Resource
    private CompLeaseOrderDetailService compLeaseOrderDetailService;


    @Override
    public PageInfo<CompLeaseManageEntity> getLeaseManagePageList(CompLeaseManagePageDTO compLeaseManagePageDTO) {
        Integer pageSize = compLeaseManagePageDTO.getPageSize();
        Integer page = compLeaseManagePageDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompLeaseManageEntity> compLeaseManageEntities =
                baseDao.getLeaseManagePageList(compLeaseManagePageDTO.getCompId(), compLeaseManagePageDTO.getParam());
        return new PageInfo<>(compLeaseManageEntities);
    }

    @Override
    public CompLeaseManageVO getLeaseManageById(Long compId, Long id) {
        CompLeaseManageVO compLeaseManageVO = new CompLeaseManageVO();
        compLeaseManageVO.setCompLeaseManageEntity(baseDao.getLeaseManageById(compId, id));
        compLeaseManageVO.setCompLeaseManageDetailEntityList(baseDao.getManageDetailById(compId, id));

        return compLeaseManageVO;
    }

    @Override
    public void addLeaseManage(LeaseManageAddOrUpdateDTO leaseManageAddOrUpdateDTO) {
        CompLeaseManageEntity compLeaseManageEntity = ConvertUtils
                .sourceToTarget(leaseManageAddOrUpdateDTO.getCompLeaseManageDTO(), CompLeaseManageEntity.class);
        insert(compLeaseManageEntity);
        List<CompLeaseManageDetailEntity> compLeaseOrderDetailEntityList = ConvertUtils
                .sourceToTarget(leaseManageAddOrUpdateDTO.getCompLeaseManageDetailDTOList(), CompLeaseManageDetailEntity.class);
        compLeaseOrderDetailEntityList.forEach(item -> {
            item.setIsDeleted(IsDeleteEnum.NORMAL.value());
            item.setLeaseManageId(compLeaseManageEntity.getId());
        });
        compLeaseManageDetailService.insertBatch(compLeaseOrderDetailEntityList);

    }

    @Override
    public void updateLeaseManage(LeaseManageAddOrUpdateDTO leaseManageAddOrUpdateDTO) {
        CompLeaseManageEntity compLeaseManageEntity = ConvertUtils
                .sourceToTarget(leaseManageAddOrUpdateDTO.getCompLeaseManageDTO(), CompLeaseManageEntity.class);
        updateById(compLeaseManageEntity);

        List<CompLeaseManageDetailEntity> compLeaseManageDetailEntityList= compLeaseManageDetailService.list(new QueryWrapper<CompLeaseManageDetailEntity>()
                .lambda().eq(CompLeaseManageDetailEntity::getLeaseManageId, compLeaseManageEntity.getId()));
        compLeaseManageDetailEntityList.forEach(item -> {
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
        });
        compLeaseManageDetailService.updateBatchById(compLeaseManageDetailEntityList);
        List<CompLeaseManageDetailEntity> compLeaseOrderDetailEntityList = ConvertUtils
                .sourceToTarget(leaseManageAddOrUpdateDTO.getCompLeaseManageDetailDTOList(), CompLeaseManageDetailEntity.class);
        compLeaseOrderDetailEntityList.forEach(item -> {
            item.setIsDeleted(IsDeleteEnum.NORMAL.value());
            item.setLeaseManageId(compLeaseManageEntity.getId());
        });
        compLeaseManageDetailService.insertBatch(compLeaseOrderDetailEntityList);

    }

    @Override
    public void deleteLeaseManage(Long compId, Long id) {
        List<CompLeaseManageDetailEntity> compLeaseManageDetailEntityList= compLeaseManageDetailService.list(new QueryWrapper<CompLeaseManageDetailEntity>()
                .lambda().eq(CompLeaseManageDetailEntity::getLeaseManageId, id));
        compLeaseManageDetailEntityList.forEach(item -> {
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
        });
        compLeaseManageDetailService.updateBatchById(compLeaseManageDetailEntityList);
        CompLeaseManageEntity compLeaseManageEntity = baseDao.selectById(id);
        compLeaseManageEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
        updateById(compLeaseManageEntity);
    }

}
